<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
			<a-col :md="8" :sm="24">
			  <a-form-item label="用户id">
			    <a-input-search allowClear v-model="queryParam.userid" placeholder="请输入用户id" />
			  </a-form-item>
			</a-col>   
            <a-col :md="8" :sm="24">
              <a-form-item label="姓名/昵称">
                <a-input-search allowClear v-model="queryParam.keyword" placeholder="姓名/昵称" />
              </a-form-item>
            </a-col>
       

            <a-col :md="8" :sm="24">
              <a-form-item label="来源">
                <a-select placeholder="全部" v-model="queryParam.platform" allowClear>
                  <a-select-option :value="-1">全部</a-select-option>
                  <a-select-option :value="0">绑卡</a-select-option>
                  <a-select-option :value="1">充值</a-select-option>
                  <!-- <a-select-option :value="2">消费</a-select-option> -->
                </a-select>
              </a-form-item>
            </a-col>
<!-- 
            <a-col :md="8" :sm="24">
              <a-form-item label="下单时间">
                <a-range-picker
                  format="YYYY-MM-DD HH:mm:ss"
                  style="width: 100%"
                  @change="onChangeTime"
                  @calendarChange="calendarChange"
                  :ranges="ranges"
                />
              </a-form-item>
            </a-col> -->

            <a-col :md="8" :sm="24">
              <a-form-item label="到账时间">
                <a-range-picker
                  format="YYYY-MM-DD HH:mm:ss"
                  style="width: 100%"
                  @change="onChangeTime2"
                  @calendarChange="calendarChange2"
                  :ranges="ranges"
                />
              </a-form-item>
            </a-col>

            
            <a-col :md="8" :sm="24">
              <a-form-item label="">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>
    <a-card :bordered="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group style="float: left" @change="$refs.table.refresh(true)" v-model="tableStatus">
          <a-radio-button :value="-1"> 全部</a-radio-button>
          <a-radio-button :value="1"> 释放 </a-radio-button>
          <a-radio-button :value="0"> 立结 </a-radio-button>
         
        </a-radio-group>

    
      </div>

      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group style="float: left" @change="$refs.table.refresh(true)" v-model="tableStatus2">
          <a-radio-button :value="-1"> 全部</a-radio-button>
          <a-radio-button :value="1"> 绑卡 </a-radio-button>
          <a-radio-button :value="0"> 充值 </a-radio-button>
         
        </a-radio-group>

    
      </div>

      <a-alert
        v-if="selectedRowKeys.length"
        style="margin-bottom: 16px"
        :message="`已选择 ${selectedRowKeys.length} 项`"
        type="info"
        show-icon
      />
      <s-table
        ref="table"
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
        :rowSelection="rowSelection"
        :columns="columns"
        :data="loadData"
        showPagination="auto"
      >
        <!-- 基本信息 -->
        <span slot="baseinfo" slot-scope="text, record">
          <a-space>
            <img style="width: 48px; heigth: 48px" :src="record.avatar_url" />
            <span>
              <a-tooltip>
                <template slot="title">
                  {{ record.nickname }}
                </template>
                <a-row
                  ><div class="over_e">{{ record.nickname }}</div></a-row
                >
              </a-tooltip>
              <a-row v-if="record.platform == 0">微信</a-row>
              <a-row v-if="record.platform == 1">支付宝</a-row>
              <a-row v-if="record.platform == 2">公众号</a-row>
            </span>
          </a-space>
        </span>
        <!-- 手机号 -->
        <span slot="wayinfo" slot-scope="text, record">
          <div>{{ text || '-' }}</div>
          <div>{{ record.yy_mobile || '-' }}</div>
        </span>
        <!-- 审核时间 -->
        <span slot="time" slot-scope="text, record">
          <!-- <a-space> 申请时间：{{ text }} </a-space> -->
          <a-space>{{ record.updated_at }} </a-space>
        </span>
        <!-- 状态 -->
        <!-- <span slot="status" slot-scope="status">
          <a-badge
            :status="status == 0 ? 'default' : status == 1 ? 'default':status == 2 ? 'error' : 'success'"
            :text="status == 0 ? '待绑卡' : status == 1 ? '待审核':status == 2 ? '已拒绝' : '已完成'"
          />
        </span> -->
        <!-- 操作 -->
        <span slot="action" slot-scope="text, record">
          <a-space>
            <a @click="handleStatus(record, 1)">通过</a>
            <a-divider type="vertical" />
            <a @click="handleStatus(record, 2)">拒绝</a>
          </a-space>
        </span>
      </s-table>
    </a-card>

    <a-modal v-model="visible" title="下级用户">
      <table>
        <tr>
          <th class="table-style">序号</th>
          <th class="table-style">分销商</th>
          <th class="table-style">下线等级</th>
          <th class="table-style">昵称</th>
        </tr>
        <tr
          v-if="now_level == 1 && first.length > 0"
          :value="item.nickname"
          v-for="(item, index) of first"
          :key="index"
        >
          <td class="table-style">{{ index }}</td>
          <td class="table-style">{{ records.nickname }}</td>
          <td class="table-style">一级</td>
          <td class="table-style">{{ item }}</td>
        </tr>
        <tr
          v-if="now_level == 2 && second.length > 0"
          :value="item.nickname"
          v-for="(item, index) of second"
          :key="index"
        >
          <td class="table-style">{{ index }}</td>
          <td class="table-style">{{ records.nickname }}</td>
          <td class="table-style">二级</td>
          <td class="table-style">{{ item }}</td>
        </tr>
        <tr
          v-if="now_level == 3 && third.length > 0"
          :value="item.nickname"
          v-for="(item, index) of third"
          :key="index"
        >
          <td class="table-style">{{ index }}</td>
          <td class="table-style">{{ records.nickname }}</td>
          <td class="table-style">三级</td>
          <td class="table-style">{{ item }}</td>
        </tr>
      </table>
    </a-modal>
    <a-modal v-model="qrcode_visiable" title="分销二维码" :footer="null">
      <img :src="qrcode_img" />
    </a-modal>
  </page-header-wrapper>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { selfAwardList } from '@/api/subscribe'
import NoBorderRadio from '@/components/NoBorderRadio/NoBorderRadio'

const columns = [
  {
    title: '用户ID',
    dataIndex: 'user_id',
    scopedSlots: { customRender: 'user_id' },
    width: 80,
  },
  {
    title: '会员信息',
    dataIndex: 'nickname',
    width: 220,
    scopedSlots: { customRender: 'baseinfo' },
    ellipsis: true,
  },

  {
    title: '结算类型',
    dataIndex: 'share_type',
    ellipsis: true,
    // width: 80,
  },
  {
    title: '结算状态',
    dataIndex: 'share_status',
    ellipsis: true,
  },
  {
    title: '佣金金额',
    dataIndex: 'money',
    ellipsis: true,
    // width: 80,
  },
  {
    title: '来源',
    dataIndex: 'pl_type',
    ellipsis: true,
    // width: 200,
  },
  {
    title: '绑卡号码',
    dataIndex: 'bind_card_no',
    ellipsis: true,
    // width: 200,
  },

  {
    title: '订单信息',
    dataIndex: 'goods_name',
    ellipsis: true,
    scopedSlots: { customRender: 'wayinfo' },
        width: 200,
  },
  {
    title: '下单时间',
    dataIndex: 'pay_time',
    sorter: (a, b) => a.created_at - b.created_at,
  },
  {
    title: '到账时间',
    dataIndex: 'date',
    sorter: (a, b) => a.sale_time - b.sale_time,
    // width: 250,
    // scopedSlots: { customRender: 'time' },
  },
]

export default {
  name: 'ShareUserList',
  components: {
    STable,
    Ellipsis,
    NoBorderRadio,
  },
  data() {
    this.columns = columns
    return {
      options: [
        {
          title: '全部',
          value: -1,
        },
        {
          title: '减少',
          value: 0,
        },
        {
          title: '增加',
          value: 1,
        },
      
      ],
      qrcode_img: '',
      qrcode_visiable: false,
      now_level: String,
      // create model
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      records: {},
      first: {},
      second: {},
      third: {},
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {
        dateStart: null,
        dateEnd: null,
        dateStart2: null,
        dateEnd2: null,
        keyword: null,
        phone: null,
        platform:-1,
      },
      selectDate: null,
      selectDate2: null,
      sellerComments: '',
      tabKey: '',
      tableStatus: -1,
      tableStatus2: -1,
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        Object.assign(parameter, this.queryParam, { status: this.tableStatus })
        Object.assign(parameter, this.queryParam, { type: this.tableStatus2 })
        console.log('loadData request parameters:', parameter)
        parameter = { ...parameter, status: this.tableStatus,type: this.tableStatus2 }
        return selfAwardList(parameter).then((res) => {
          console.log(res)
          return res.data
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      ranges: {
        一月内: () => [moment().startOf('day').subtract(1, 'month'), moment()],
        昨天: () => [moment().startOf('day').subtract(1, 'days'), moment().endOf('day').subtract(1, 'days')],
        最近一周: () => [moment().startOf('day').subtract(1, 'weeks'), moment()],
      },
    }
  },
  filters: {},
  created() {},
  watch: {
    selectedRowKeys(newValue, oldValue) {
      console.log(newValue, oldValue)
    },
  },
  computed: {
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange,
      }
    },
  },
  methods: {
    calendarChange(date, dateString) {
    
      this.selectDate = date[0]
    },
    calendarChange2(date, dateString) {
    
    this.selectDate2 = date[0]
  },
    qrcode(val) {
      console.log(val)
      var params = {
        user_id: val,
      }
      getQrcode(params)
        .then((res) => {
          if (res.code !== 0) {
            this.$error({
              content: res.msg,
            })
          } else {
            this.qrcode_img = res.data.url
            this.qrcode_visiable = true
          }
        })
        .catch((err) => {
          this.$message.error(err.message)
        })
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
        // 选择后的事件
    onChangeTime(date, dateString) {
      this.queryParam.dateStart = dateString[0]
      this.queryParam.dateEnd = dateString[1]
    },
    onChangeTime2(date, dateString) {
      this.queryParam.dateStart2 = dateString[0]
      this.queryParam.dateEnd2 = dateString[1]
    },
    resetSearchForm() {
      this.queryParam = {}
    },
    tabChange(key) {
      this.queryParam.status = key
      this.$refs.table.refresh(true)
    },
    getLevel(record, level) {
      this.records = record
      this.first = record.first.team
      this.second = record.second.team
      this.third = record.third.team
      this.now_level = level
      this.visible = true
    },
    setComments(record) {
      var params = {
        id: record.id,
        seller_comments: this.sellerComments,
      }
      setComments(params)
        .then((res) => {
          if (res.code !== 0) {
            this.$error({
              content: res.msg,
            })
          } else {
            this.$message.success('备注成功')
            this.$refs.table.refresh()
          }
          this.sellerComments = ''
        })
        .catch((err) => {
          this.$message.error(err.message)
        })
    },
    handleStatus(record, status) {
      var params = {
        id: record.id,
        status: status,
      }
      var returnMsg = '拒绝成功！'
      if (status === 1) {
        returnMsg = '通过成功！'
      }
      setAudit(params).then((res) => {
        this.modalLoading = false
        if (res.code !== 0) {
          this.$error({
            content: res.msg,
          })
        } else {
          this.$refs.table.refresh()
          this.$message.success(returnMsg)
        }
      })
    },
    handleDelete(record) {
      var params = {
        id: record.id,
      }
      setDel(params).then((res) => {
        this.modalLoading = false
        if (res.code !== 0) {
          this.$error({
            content: res.msg,
          })
        } else {
          this.$refs.table.refresh()
          this.$message.success('删除成功！')
        }
      })
    },
    jump(record, type) {
      if (type === 1) {
        this.$router.push({ name: 'ShareOrder', query: { parent_id: record.user_id } })
      } else {
        this.$router.push({ name: 'ShareCash', query: { id: record.id } })
      }
    },
    // 点击批量操作
    handleMenuClick(e) {
      let status = e.key
      if (this.selectedRowKeys.length > 0) {
        var params = {
          id: this.selectedRowKeys,
          status: status,
        }
        var returnMsg = '拒绝成功！'
        if (status === 1) {
          returnMsg = '通过成功！'
        }
        setAudit(params).then((res) => {
          this.modalLoading = false
          if (res.code !== 0) {
            this.$error({
              content: res.msg,
            })
          } else {
            this.$refs.table.refresh()
            this.$message.success(returnMsg)
          }
        })
      }
    },
  },
}
</script>
<style lang="less" scoped>
.top_card {
  /deep/.ant-card-body {
    padding-bottom: 0;
  }
  /deep/.ant-tabs-bar {
    margin-bottom: 0;
    border-bottom: 1px solid #fff;
  }
}
.button_style {
  margin-top: 3px;
}
.table-style {
  padding: 0 30px;
}
.over_e {
  width: 90px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
</style>
